﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HtmlDoc = HtmlAgilityPack.HtmlDocument;
using WebFunctions;
using Newtonsoft.Json;

namespace AutoIndustryFiles
{
    class emersonNwScraper : FileLinksScraper2
    {
        public emersonNwScraper()
        {
            this.MainPage = "http://www.emersonnetworkpower.com.cn/Product/Pages/default.aspx";
            this.CompanyName = "爱默生网络能源有限公司";
            this.CompanyUrl = "http://www.emersonnetworkpower.com.cn/";
            this.ColumnName = "首页-> 产品";
            //this.ReviseXPath += new OnReviseXPath(FuJiScraper_ReviseXPath);            
        }

        public override void GenerateAllLinks(string param)
        {
            string Url = @"http://www.emersonnetworkpower.com.cn/Product/Pages/default.aspx";


            FileLink lnk = new FileLink();
            lnk.Id = "0";
            lnk.Name = "产品";
            lnk.CategoryPath = "";
            lnk.IsFileLink = false;
            lnk.Url = Url;
            FileLinks.Add(lnk);

            HtmlDoc doc = new HtmlDoc();
            doc.LoadHtml(WebFuncs.OpenUrlEx(Url, "UTF-8"));

            GenerateSubWebLinks(doc, ref lnk, @"//div[@id=""zz1_TopNavigationMenun3Items""]//a", null, null);

            while (FileLinksToScrape.Count > 0)
            {
                FileLink lnk2 = FileLinksToScrape.Dequeue();
                bool f = false;
                UpdateMsg("emersonNwScraper", lnk2.Url, ref f);
                Application.DoEvents();

                doc = new HtmlDoc();
                doc.LoadHtml(WebFuncs.OpenUrlEx(lnk2.Url, "UTF-8"));

                GenerateSubWebLinks(doc, ref lnk2, @"//table[@id=""zz2_CurrentNav""]/tr[not(@id)]//a",OnLeafNode, null);
            }            
        }

        void OnLeafNode(HtmlDoc doc_obj,ref FileLink lnkParent)
        {
            //生成文件列表
            HtmlAgilityPack.HtmlNodeCollection hnc = doc_obj.DocumentNode.SelectNodes(@"//div[@class=""TabbedPanelsContent""]//a");

            if (hnc == null)
                return;

            foreach (HtmlAgilityPack.HtmlNode node in hnc)
            {                
                FileLink lnk_new = new FileLink();
                lnk_new.Id = FileLinks.Count.ToString();
                lnk_new.Name = node.InnerText;
                if (lnk_new.Name.LastIndexOf(".") > 0)
                    lnk_new.FileType = lnk_new.Name.Substring(lnk_new.Name.LastIndexOf(".")+1);
                lnk_new.ReleaseDate = "";
                lnk_new.Url = node.GetAttributeValue("href","");
                lnk_new.Url = FormatUrl(lnk_new.Url, lnkParent.Url);
                lnk_new.IsFileLink = true;
                lnk_new.CategoryPath = lnkParent.CategoryPath + "->" + lnkParent.Name;
                FileLinks.Add(lnk_new);
            }

        }
    }
}
